<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page language="java" import="java.lang.*" %>
<%@ page import = "java.sql.SQLException" %>
<%@ page import = "com.seniorproject.aims.*" %>
<%@ page import = "java.util.List" %>
<%@page import="java.util.ArrayList"%>
<%@ page import = "java.util.Properties" %>
<%@ page import = "javax.servlet.ServletContext" %>
<%@ page import = "java.io.File" %>
<%@ page import = "java.io.InputStream" %>

<html>
<head>
			<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
			<title>Home</title>
			<link rel="stylesheet" type="text/css" href="css/stylev2.css" />
			<link rel="stylesheet" type="text/css" href="css/menubar.css" />
			<link rel="stylesheet" type="text/css" href="css/student-system.css" />
			<script type="text/javascript" src="js/jquery-2.1.1.min.js"></script>
			<script type="text/javascript" src="js/bootbox.js"></script>
			
			<style type="text/css">
				a {
			      text-decoration:none;
			   }
			</style>
			
</head>
<body>
<%
//Get session
	String strUser = String.valueOf(session.getAttribute("sUser"));
	HttpSession htp_session = request.getSession();
			
	if (htp_session == null || htp_session.getAttribute("sUser") == null) {
	    // Forward the control to login.jsp if authentication fails or session expires
	    request.getRequestDispatcher("/login.jsp").forward(request,
	        response);
	}	
	
	if("admin".equals(htp_session.getAttribute("sUser"))){%>
	<script>alert("This user dont have PERMISSION to access this zone.");</script>
	<meta HTTP-EQUIV="Refresh" CONTENT="0; URL=admin_home.jsp"> 	
	<%}
%>
<%	
///////////////////////////////////////////////////////////////////////////////////////////////////////////
//GET PERMISSION

			String name = String.valueOf(session.getAttribute("name"));
			String surname = String.valueOf(session.getAttribute("surname"));
			String role_name = String.valueOf(session.getAttribute("curriculum_role"));
			
			String department_id = String.valueOf(session.getAttribute("department_id"));

			int num_system = Integer.parseInt(String.valueOf(session.getAttribute("num_system")));
					
			System.out.println("name : " + name);///////////////////////////////
			System.out.println("department_id : " + department_id);/////////////////////////
			
			List<String> system_name = new ArrayList<String>();
			
			Permission permission = new Permission(strUser,name);
			permission.setCurriculumCreate(Boolean.valueOf(String.valueOf(session.getAttribute("curriculum_create"))));
			permission.setCurriculumDelete(Boolean.valueOf(String.valueOf(session.getAttribute("curriculum_delete"))));
			permission.setCurriculumRead(Boolean.valueOf(String.valueOf(session.getAttribute("curriculum_read"))));
			permission.setCurriculumUpdate(Boolean.valueOf(String.valueOf(session.getAttribute("curriculum_update"))));
			
			String permiss = "";
			if(permission.getCurriculumCreate()) {
				permiss = permiss + "Add";
			}
			if(permission.getCurriculumUpdate()) {
				if("".equals(permiss)) {
					permiss = permiss + "Edit";
				} else {
					permiss = permiss + " Edit";
				}
			}
			if(permission.getCurriculumRead()) {
				if("".equals(permiss)) {
					permiss = permiss + "Read";
				} else {
					permiss = permiss + " Read";
				}
			}
			if(permission.getCurriculumDelete()) {
				if("".equals(permiss)) {
					permiss = permiss + "Delete";
				} else {
					permiss = permiss + " Delete";
				}
			}
			
///////////////////////////////////////////////////////////////////////////////////////////////////////////			
		//check permission
			if(!permission.getCurriculumRead()){%>
				<script>alert("This user dont have PERMISSION to access this zone.");</script>
				<meta HTTP-EQUIV="Refresh" CONTENT="0; URL=curriculum_home.jsp"> 	
			<%}

			//set Database Connection
			String hostProps = "";
			String usernameProps  = "";
			String passwordProps  = "";
			String databaseProps = "";
			

try {
	//get current path
	ServletContext servletContext = request.getSession().getServletContext();
	
	InputStream input = servletContext.getResourceAsStream("/properties/connectDB.properties");
	Properties props = new Properties();
	
	props.load(input);

	hostProps  = props.getProperty("host");
	usernameProps  = props.getProperty("username");
	passwordProps  = props.getProperty("password");
	databaseProps = props.getProperty("database");
} catch (Exception e) { 
	out.println(e);  
}

System.out.println("hostProperties : "+hostProps);////////////////try/////////////////
System.out.println("usernameProperties : "+usernameProps);////////////////try/////////////////
System.out.println("passwordProperties : "+passwordProps);////////////////try/////////////////
System.out.println("databaseProperties : "+databaseProps);////////////////try/////////////////

// connect database
		Connection connect = null;		
		try {
			Class.forName("com.mysql.jdbc.Driver");
		
			connect =  DriverManager.getConnection("jdbc:mysql://" + hostProps  + "/" + databaseProps +
					"?user=" + usernameProps  + "&password=" + passwordProps + "&characterEncoding=tis620");//////"&characterEncoding=tis620" Encoding Thai);
		
			if(connect != null){
				System.out.println("Database Connect Sucesses.");
			} else {
				System.out.println("Database Connect Failed.");	
			}
		
		} catch (Exception e) {
			out.println(e.getMessage());
			e.printStackTrace();
		}	
		
	/* find Curriculum in database */
	List<Curriculum> curriculum = new ArrayList<Curriculum>();
	
	try {			   
		ResultSet rs_curriculum = connect.createStatement().executeQuery("SELECT `curriculum_id`, `curriculum_name_th`, `curriculum_name_en`, `bachelor_name_th`, `bachelor_name_en`, `modified`, `department_id`, `credit`, `year`, `co_organizer`, `degree`, `program`, `ohce_approved`, `onec_approved`, `curriculum_EngApp` " 
				+ " FROM curriculum " + " ORDER BY curriculum_id");
		
		while(rs_curriculum.next()) {
			Curriculum cur = new Curriculum();
			
			cur.setCurriculumNameTH(rs_curriculum.getString("curriculum_name_th"));
			cur.setCurriculumNameEN(rs_curriculum.getString("curriculum_name_en"));
			cur.setCurriculumId(Integer.parseInt(rs_curriculum.getString("curriculum_id")));
			cur.setBachelorNameTH(rs_curriculum.getString("bachelor_name_th"));
			cur.setBachelorNameEN(rs_curriculum.getString("bachelor_name_en"));
			cur.setModified(rs_curriculum.getString("modified"));
			cur.setCredit(Integer.parseInt(rs_curriculum.getString("credit")));
			cur.setDepartmentId(Integer.parseInt(rs_curriculum.getString("department_id")));
			cur.setYear(rs_curriculum.getString("year"));
			cur.setCoorganizer(rs_curriculum.getString("co_organizer"));
			cur.setDegree(rs_curriculum.getString("degree"));
			cur.setProgram(rs_curriculum.getString("program"));
			cur.setOHCE(rs_curriculum.getString("ohce_approved"));
			cur.setONEC(rs_curriculum.getString("onec_approved"));
			cur.setEngApp(rs_curriculum.getString("curriculum_EngApp"));
			
			curriculum.add(cur);
		}
		
	} catch (SQLException e) {
		e.printStackTrace();
	}
	
	connect.close();
	%>	

	
	<div id="page-wrap">
		<div id="inside">
			<table border="0">
				<tr style="font-size:24px">
					<td width="600" height="100" style= "background-color: #273b80;border: 0px ;padding: 0px; 0px;" >
						<div id="header_banner-2"></div>
					</td>
					
					<td width="680" height="100" style= "background-color: #273b80;border: 0px;padding: 0px; 0px;" >
						<p align="right"><label><font size="4" color="#ffffff">username : <%=strUser %> &nbsp;&nbsp;</font></label></p>	
						<a href="logout.jsp"><p align="right"><label style="cursor: pointer;"><font size="3" color="#ffffff">log out &nbsp;</font></label></p></a>
					</td>	
				</tr>
			</table>
			<table border="0">
				<tr style="font-size:10px">
					<td width = "720" height="50" style= "background-color:#FFFFFF; border-bottom-color: #FFFFFF; border-left-color: #FFFFFF;">
						<center><h2><label>หน้าหลัก </label></h2></center>
					</td>
<%
			if(permission.getCurriculumRead()) {
%>					
					<td  width = "720" height="50" style= "background-color:#D0D0D0 ;">
						<center><a href="curriculum_search.jsp"><h2><label style="cursor: pointer;">ค้นหา</label></h2></a></center>
					</td>
<%
			}
			if(permission.getCurriculumCreate()) {
%>						
					<td width = "720" height="50" style= "background-color:#D0D0D0 ;">
						<center><a href="curriculum_import.jsp"><h2><label style="cursor: pointer;">เพิ่มรายวิชา</label></h2></a></center>
					</td>
					<td  width = "720" height="50" style= "background-color:#D0D0D0 ;">
						<center><a href="curriculum_add_curriculum.jsp"><h2><label style="cursor: pointer;">เพิ่มหลักสูตรใหม่</label></h2></a></center>
					</td>
					<td  width = "720" height="50" style= "background-color:#D0D0D0 ;">
						<center><a href="curriculum_modified_home.jsp"><h2><label style="cursor: pointer;">ปรับปรุงหลักสูตร</label></h2></a></center>
					</td>
<%
			}
			if(permission.getCurriculumUpdate()) {
%>
					<td  width = "720" height="50" style= "background-color:#D0D0D0 ;">
						<center><a href="curriculum_edit_home.jsp"><h2><label style="cursor: pointer;">แก้ไขข้อมูลหลักสูตร</label></h2></a></center>
					</td>
<%
			}
%>
				</tr>
			</table>
			
			<div id="main-content-ac">
			
				</br></br>
					
						<h2 style="padding-left: 2.5cm; color: #273b80; font-size: 24px;">ยินดีต้อนรับ : <%=permission.getName()%> </h2>
						
						</br></br></br></br>
						
						<center>
						
								<table id="detail_table" width="1000" border="1">
						        	<tr style="font-size:24px" >
						        		<td  width="50" height="50" rowspan="4" style="text-align: center;">
						        			<img id="user_image" src="image/user_no_photo.jpg" style="width:130px;height:160px;	">
						        		</td>
						             	<td  width="500" >
						                	<font size="5"><strong>ชื่อ</strong></font>
						                </td>
						                <td width="400" height="50">
						                	<%=name + " " + surname %>
						                </td>
						            </tr>
						                
						                
									<tr style="font-size:24px">
						                <td height="50">
						                	<font size="5"><strong>ตำแหน่ง</strong></font>
						                </td>
						                <td height="50">
						                	<%= role_name %>
						                </td>
						            </tr>
						                
									<tr style="font-size:24px">
						                <td height="50">
						                	<font size="5"><strong>สิทธิการใช้งานในระบบ</strong></font>
						                </td>
						                <td height="50">
						                	<%=permiss %>
						                </td>
						            </tr>
						            
						            <tr style="font-size:24px">
						                <td height="50">
						<%
									if(num_system > 1){
						%>
										<center><a href="system_select.jsp"><font size="3" color="#ffffff"><input type="button" value="เปลี่ยนระบบ" class="buttonStyle" style="cursor: pointer;width:200px;"/></font></a></center>
						<%
									}
						%>						                	
						                </td>
						                <td height="50">
						                	<form id="form1" name="form1" method="post" action="change_password.jsp">
												<input type="hidden" id="mode" name="mode" value="curriculum" /> 
												<input type="hidden" id="system" name="system" value="curriculum" /> 
												<font size="3" color="#ffffff"><input type="submit" value="เปลี่ยนรหัสผ่าน" class="buttonStyle" style="cursor: pointer;width:200px;"/></font>
												
											</form>
						                </td>
						            </tr>
						                 
								</table>			
				</center>
				</br></br></br>
			<div style="clear: both;"></div>
			
				
			<div id="footer"></div>			
		</div>
		
			<div style="clear: both;"></div>
	</div>		

</body>
</html>